﻿/*
 * AlgAnalysis.java
 *
 * Created on 2007年7月20日, 上午 11:53
 *
 */

package org.uranus.core.ssh.v2;

import java.util.Vector;
import org.uranus.core.ssh.SSHConnectionException;
import org.uranus.core.uitl.StringTokenizer;

/**
 * Copyright 2007 Hsiang-Yu Wang<br>
 * This file is distributed under the terms of the GNU General Public License<br>
 *<br>
 *<pre>
 * --------------------License notice--------------------
 * GNU GENERAL PUBLIC LICENSE
 * Version 3, 29 June 2007
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not,
 * visit: http://www.gnu.org/licenses/gpl.txt
 * --------------------License notice--------------------</pre>
 * @author Hsiang-Yu Wang
 */
public class AlgAnalysis{
    public Vector payload;
    public byte[] cookie;
    
    private String kex_alg;
    private String server_host_key_alg;
    private String encryption_alg_client_to_server;
    private String encryption_alg_server_to_client;
    private String mac_alg_client_to_server;
    private String mac_alg_server_to_client;
    private String compression_alg_client_to_server;
    private String compression_alg_server_to_client;
    private String languages_client_to_server;
    private String languages_server_to_client;
    
    
    /** Creates a new instance of AlgAnalysis */
    public AlgAnalysis() {
        setPayload(null);
    }
    public AlgAnalysis(SshPacketV2 p) throws SSHConnectionException{
        setPayload(new Vector());
        for(int i = 1 ; i < p.getPayload_length() ; i++){
            getPayload().addElement(new Byte(p.getPayload(i)));
        }
        setCookie(new byte[16]);
        for(int i = 0 ; i < getCookie().length ; i ++){
            Byte b = (Byte)(getPayload().firstElement());
            getCookie()[i] = b.byteValue();
            getPayload().removeElementAt(0);
        }
        assignString();
    }
    
    private void assignString() throws SSHConnectionException{
        kex_alg = allocateString();
        server_host_key_alg = allocateString();
        if ( !algCheck(server_host_key_alg, "ssh-dss")){
            throw new SSHConnectionException("Alogorithm confused");
        }
        encryption_alg_client_to_server = allocateString();
        encryption_alg_server_to_client = allocateString();
        mac_alg_client_to_server = allocateString();
        mac_alg_server_to_client = allocateString();
        compression_alg_client_to_server = allocateString();
        compression_alg_server_to_client = allocateString();
        languages_client_to_server = allocateString();
        languages_server_to_client = allocateString();
        
    }
    private boolean algCheck(String in, String check){
        StringTokenizer st = new StringTokenizer(in, ',');
        while(st.hasMoreTokens()){
            if(st.nextToken().equals(check)){
                return true;
            }
        }
        return false;
    }
    private String allocateString(){
        byte[] temp = new byte[4];
        for(int i = 0 ; i < 4 ; i ++){
            Byte b = (Byte)payload.firstElement();
            payload.removeElementAt(0);
            temp[i] = b.byteValue();
        }
        int length = calculateInt32(temp);
        temp = new byte[length];
        for(int i = 0 ; i < temp.length ; i ++){
            Byte b = (Byte)payload.firstElement();
            payload.removeElementAt(0);
            temp[i] = b.byteValue();
        }
        String str = new String(temp);
        return str;
        
    }
    
    private int calculateInt32(byte[] in){
        int length = 0;
        int j = 2;
        for(int i = 0 ; i < 2 ; i++){
            int temp = (in[j] & 0xFF);
            length += temp*((256 >> (8*i)) & 0xFFFF);
            j++;
        }
        return length;
    }
    public DHKex initDHKex() throws SSHConnectionException{
        StringTokenizer st = new StringTokenizer(kex_alg, ',');
        while(st.hasMoreTokens()){
            if(st.nextToken().equals("diffie-hellman-group1-sha1")){
                return new DHKex(1);
            }
        }
        throw new SSHConnectionException("Algorithm DIS-MATCHED");
    }
    
    /////////////////// Getter / Setter ////////////////////////////////
    ////////////////////////////////////////////////////////////////////
    public String getKex_alg() {
        return kex_alg;
    }
    
    public void setKex_alg(String kex_alg) {
        this.kex_alg = kex_alg;
    }
    
    public String getServer_host_key_alg() {
        return server_host_key_alg;
    }
    
    public void setServer_host_key_alg(String server_host_key_alg) {
        this.server_host_key_alg = server_host_key_alg;
    }
    
    public String getEncryption_alg_client_to_server() {
        return encryption_alg_client_to_server;
    }
    
    public void setEncryption_alg_client_to_server(String encryption_alg_client_to_server) {
        this.encryption_alg_client_to_server = encryption_alg_client_to_server;
    }
    
    public Vector getPayload() {
        return payload;
    }
    
    public void setPayload(Vector payload) {
        this.payload = payload;
    }
    
    public byte[] getCookie() {
        return cookie;
    }
    
    public void setCookie(byte[] cookie) {
        this.cookie = cookie;
    }
    
    public String getEncryption_alg_server_to_client() {
        return encryption_alg_server_to_client;
    }
    
    public void setEncryption_alg_server_to_client(String encryption_alg_server_to_client) {
        this.encryption_alg_server_to_client = encryption_alg_server_to_client;
    }
    
    public String getMac_alg_client_to_server() {
        return mac_alg_client_to_server;
    }
    
    public void setMac_alg_client_to_server(String mac_alg_client_to_server) {
        this.mac_alg_client_to_server = mac_alg_client_to_server;
    }
    
    public String getMac_alg_server_to_client() {
        return mac_alg_server_to_client;
    }
    
    public void setMac_alg_server_to_client(String mac_alg_server_to_client) {
        this.mac_alg_server_to_client = mac_alg_server_to_client;
    }
    
    public String getCompression_alg_client_to_server() {
        return compression_alg_client_to_server;
    }
    
    public void setCompression_alg_client_to_server(String compression_alg_client_to_server) {
        this.compression_alg_client_to_server = compression_alg_client_to_server;
    }
    
    public String getCompression_alg_server_to_client() {
        return compression_alg_server_to_client;
    }
    
    public void setCompression_alg_server_to_client(String compression_alg_server_to_client) {
        this.compression_alg_server_to_client = compression_alg_server_to_client;
    }
    
    public String getLanguages_client_to_server() {
        return languages_client_to_server;
    }
    
    public void setLanguages_client_to_server(String languages_client_to_server) {
        this.languages_client_to_server = languages_client_to_server;
    }
    
    public String getLanguages_server_to_client() {
        return languages_server_to_client;
    }
    
    public void setLanguages_server_to_client(String languages_server_to_client) {
        this.languages_server_to_client = languages_server_to_client;
    }
    
    
}
